package cn.itcast.demo;

import cn.itcast.demo.pojo.Heima56;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.junit.Test;

import java.util.List;
import java.util.Map;

public class SolrIndexQueryTest {


    @Test
    public void testQueryIndex() throws SolrServerException {
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");

        //创建查询条件对象
        SolrQuery solrQuery = new SolrQuery("title:手机");

        //执行查询并得到响应
        QueryResponse queryResponse = solrServer.query(solrQuery);

        //从响应数据中获取结果数据
        SolrDocumentList results = queryResponse.getResults();

        results.forEach(result->{
            String id = (String) result.get("id");
            String title = (String) result.get("title");
            Long price = Long.valueOf(result.get("price").toString());

            System.out.println("id:"+id+" title:"+title+" price:"+price);
        });


    }


    @Test
    public void testQueryIndexWithBean() throws SolrServerException {
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");

        //创建查询条件对象
        SolrQuery solrQuery = new SolrQuery("id:1");

        //执行查询并得到响应
        QueryResponse queryResponse = solrServer.query(solrQuery);

        //从响应数据中获取结果数据
        List<Heima56> beans = queryResponse.getBeans(Heima56.class);

        beans.forEach(bean->{
            String id = bean.getId();
            String title = bean.getTitle();
            Long price = bean.getPrice();

            System.out.println("id:"+id+" title:"+title+" price:"+price);
        });
    }


    @Test
    public void testRangeQueryIndex() throws SolrServerException {
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");

        //创建查询条件对象,范围查询，包含左右边界
        SolrQuery solrQuery = new SolrQuery("price:[* TO 299999]");

        //执行查询并得到响应
        QueryResponse queryResponse = solrServer.query(solrQuery);

        //从响应数据中获取结果数据
        SolrDocumentList results = queryResponse.getResults();

        results.forEach(result->{
            String id = (String) result.get("id");
            String title = (String) result.get("title");
            Long price = Long.valueOf(result.get("price").toString());

            System.out.println("id:"+id+" title:"+title+" price:"+price);
        });
    }


    //AND  OR   NOT
    @Test
    public void testBooleanQueryIndexWithBean() throws SolrServerException {
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");

        //创建查询条件对象
        SolrQuery solrQuery = new SolrQuery("title:手机 AND price:[400000 TO 700000] NOT title:金立");

        //执行查询并得到响应
        QueryResponse queryResponse = solrServer.query(solrQuery);

        //从响应数据中获取结果数据
        List<Heima56> beans = queryResponse.getBeans(Heima56.class);

        beans.forEach(bean->{
            String id = bean.getId();
            String title = bean.getTitle();
            Long price = bean.getPrice();

            System.out.println("id:"+id+" title:"+title+" price:"+price);
        });
    }

    @Test
    public void testSortQueryIndexWithBean() throws SolrServerException {
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");

        //创建查询条件对象
        SolrQuery solrQuery = new SolrQuery("title:手机");

        //给查询条件指定排序条件
        solrQuery.setSort("price", SolrQuery.ORDER.desc);

        //执行查询并得到响应
        QueryResponse queryResponse = solrServer.query(solrQuery);

        //从响应数据中获取结果数据
        List<Heima56> beans = queryResponse.getBeans(Heima56.class);

        beans.forEach(bean->{
            String id = bean.getId();
            String title = bean.getTitle();
            Long price = bean.getPrice();

            System.out.println("id:"+id+" title:"+title+" price:"+price);
        });
    }

    @Test
    public void testPageQueryIndexWithBean() throws SolrServerException {

        int currentPage = 1;
        final int PAGE_SIZE = 5;
        int start = (currentPage-1)*PAGE_SIZE;


        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");

        //创建查询条件对象
        SolrQuery solrQuery = new SolrQuery("title:手机");
        solrQuery.setStart(start);
        solrQuery.setRows(PAGE_SIZE);

        //给查询条件指定排序条件


        //执行查询并得到响应
        QueryResponse queryResponse = solrServer.query(solrQuery);

        //从响应数据中获取结果数据
        List<Heima56> beans = queryResponse.getBeans(Heima56.class);

        beans.forEach(bean->{
            String id = bean.getId();
            String title = bean.getTitle();
            Long price = bean.getPrice();

            System.out.println("id:"+id+" title:"+title+" price:"+price);
        });
    }



    @Test
    public void testLikeQueryIndexWithBean() throws SolrServerException {



        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");

        //创建查询条件对象,模糊查询本质就是≈，要模糊就需要打开模糊的开关,如果要修改最大编辑距离只需要在模糊开关后跟对应的数字就行
        SolrQuery solrQuery = new SolrQuery("title:vcva~1");


        //给查询条件指定排序条件


        //执行查询并得到响应
        QueryResponse queryResponse = solrServer.query(solrQuery);

        //从响应数据中获取结果数据
        List<Heima56> beans = queryResponse.getBeans(Heima56.class);

        beans.forEach(bean->{
            String id = bean.getId();
            String title = bean.getTitle();
            Long price = bean.getPrice();

            System.out.println("id:"+id+" title:"+title+" price:"+price);
        });
    }



    @Test
    public void testHighLightQueryIndexWithBean() throws SolrServerException {



        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");

        //创建查询条件对象
        SolrQuery solrQuery = new SolrQuery("title:手机");

        //指定高亮查询
        solrQuery.addHighlightField("title");
        solrQuery.setHighlightSimplePre("<em>");
        solrQuery.setHighlightSimplePost("</em>");


        //给查询条件指定排序条件


        //执行查询并得到响应
        QueryResponse queryResponse = solrServer.query(solrQuery);


        //双层map第一层key为高亮文档的id，第二层map的key为高亮字段的名称，值为高亮的结果，由于schema对字段的值可以多值，所以api设计为集合
        Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();



        //从响应数据中获取结果数据
        List<Heima56> beans = queryResponse.getBeans(Heima56.class);

        beans.forEach(bean->{
            String id = bean.getId();
            String highLightTitle = highlighting.get(id).get("title").get(0);
            //String title = bean.getTitle();
            Long price = bean.getPrice();

            System.out.println("id:"+id+" title:"+highLightTitle+" price:"+price);
        });
    }
}
